<!--
Copyright 2009 Johan Maasing, Mikael Forsberg

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>List stuff with dojo</title>

		<link type="text/css" rel="stylesheet" media="all" href="http://o.aolcdn.com/dojo/1.3.2/dijit/themes/tundra/tundra.css" />
		<link type="text/css" rel="stylesheet" media="all" href="http://o.aolcdn.com/dojo/1.3.2/dojox/grid/resources/Grid.css" />
		<link type="text/css" rel="stylesheet" media="all" href="http://o.aolcdn.com/dojo/1.3.2/dojox/grid/resources/tundraGrid.css" />

		<script type="text/javascript">
			var djConfig = {isDebug: true, parseOnLoad: true};
		</script>

		<script type="text/javascript" src="http://o.aolcdn.com/dojo/1.3.2/dojo/dojo.xd.js"></script>

		<script type="text/javascript">

			dojo.require("dojo.data.ItemFileWriteStore");
			dojo.require("dojox.grid.DataGrid");

			var grid = null;
			var jsonStore = null;
			var stuffUrl = "pj2r/DojoStuff";

			dojo.addOnLoad(function() {
				jsonStore = new dojo.data.ItemFileWriteStore({ url: stuffUrl });

				var addButton = dojo.byId("add-stuff-button");
				dojo.connect(addButton, 'onclick', function(e) {
					e.preventDefault();
					addItem();
				});

				var layout = [	{field: "id", width: "auto", name: "Id" },
								{field: "stuff", width: "100px", name: "Stuff", editable: true},
								{field: "id", width: "auto" , formatter: createDeleteButton, name: "Delete" }
							];

				grid = new dojox.grid.DataGrid({
					query: {id: '*' },
					store: jsonStore,
					structure: layout,
					rowsPerPage: 20
				}, 'list-container');

				grid.startup();
			});

			function createDeleteButton(id) {
				var deleteButton = "<button onclick=\"deleteItem('" + id + "')\">Delete</button>";
				return deleteButton;
			}

			function deleteItem(id) {
				var xhrDeleteArgs = {
						url: stuffUrl + "/" + id,
						handleAs: "text",
						load: function(data) {
							console.log("Deleted " + id);
							updateDataStore();
						}
				}
				dojo.xhrDelete(xhrDeleteArgs)
			}

			function addItem() {
				var xhrPostArgs = {
						url: stuffUrl,
						form: 'add-form',
						handleAs: 'text',
						load: function(data, ioargs) {
							var location = ioargs.xhr.getResponseHeader('Location');
							console.log("Created: " + location);
							updateDataStore();
						},
						error: function (data) {
							console.log("something went wrong " + data);
						}
				}
				dojo.xhrPost(xhrPostArgs);
				//updateDataStore();
			}

			function updateItem() {
				var xhrPutArgs = {
						url: stuffUrl,
						putData: "stuff=",
						handleAs: "json"
				}
				dojo.xhrPut(xhrPutArgs);
				updateDataStore();
			}

			function updateDataStore() {
				jsonStore = new dojo.data.ItemFileWriteStore({ url: stuffUrl });
				grid.setStore(jsonStore);
			}
	</script>

	</head>
	<body class="tundra">
		<h2>DOJO DataGrid CRUD</h2>
		<form action="pj2r/DojoStuff" method="post" id="add-form">
			<fieldset>
				<label for="contentField">Content:</label>
				<br />
				<input id="stuff" name="stuff" />
				<br />
				<input type="submit" value="Create new" id="add-stuff-button" />
			</fieldset>
		</form>

		<p>
			Adding Stuff will send a POST request to the server and
			 an item will be saved.
		</p>
		<p>
			Pressing Delete will send a DELETE request to the server
			and the item will be deleted
		</p>
		<p>
			Double click on a value to edit. Sends an
			PUT request to the server an updates the value.
			Not implemented yet.
		</p>

		<div id="list-container" style="width: 500px; height: 100%;">
		</div>
	</body>
</html>
